package com.nox.trademe.testapp.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.nox.trademe.testapp.R;
import com.nox.trademe.testapp.models.MainCategory;

import java.util.ArrayList;
import java.util.List;

public class MainCategoryAdapter extends ArrayAdapter<MainCategory> {
	
	private List<MainCategory> categoryList;
	private Context context;
	
	public MainCategoryAdapter(ArrayList<MainCategory> list, Context context) {
		super(context, R.layout.maincategory_item, list);
		this.categoryList = list;
		this.context = context;
	}
	
	public int getListSize() {
		if (categoryList != null)
			return categoryList.size();
		return 0;
	}

	public MainCategory getCategory(int pos) {
		if (categoryList != null)
			return categoryList.get(pos);
		return null;
		}
	
	public long getCategoryId(int pos) {
		if (categoryList != null)
			return ((Object)categoryList.get(pos)).hashCode();
		return 0;
		}
	
	public List<MainCategory> getCategoriesList() {
		return categoryList;
		}
	
	public void setCategoriesList(List<MainCategory> list) {
		this.categoryList = list;
		}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.maincategory_item, null);
		}
		
		MainCategory category = categoryList.get(position);
		TextView text = (TextView) v.findViewById(R.id.maincategory_name);
		text.setText(category.getName());
		notifyDataSetChanged();
		return v;
	}
}
